﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using gts;

namespace GCard
{
    public partial class Monitor : Form
    {
        public Monitor()
        {
            InitializeComponent();
            InitalStatusMonitor();
        }
        GtsCard gCard = new GtsCard();
        int flagAlarm, flagLmtP, flagLmtN;
        int status,
            alarm,
            error,
            positive,
            negetive,
            st,//平滑停止
            et,//急停
            en,//使能
            run,
            compare;
        int pAlarm,
            pHome,
            pLmtP,
            pLmtN;
        short sRtn, axisS;
        int prfMode = 7;
        double prfVel = 0, prfPos = 0, encPos = 0, encVel = 0;
        uint pClock;

        private void InitalStatusMonitor()
        {
            this.cmb_axisStatus.DropDownStyle = ComboBoxStyle.DropDownList;//cmb_axisStatus初始化
            this.cmb_axisStatus.Text = this.cmb_axisStatus.Items[0].ToString();//设置默认值
            timer1.Enabled = true;
        }

        private void btn_ClearStatus_Click(object sender, EventArgs e)
        {
            flagAlarm = 0;
            flagLmtP = 0;
            flagLmtN = 0;
            sRtn = mc.GT_ClrSts(0, axisS, 1);
        }

        private void StatusConvert()
        {
            if ((status & 0x0001) == 0) alarm = 0;
            else alarm = 1;
            if ((status & 0x0010) == 0) error = 0;
            else error = 1;
            if ((status & 0x0020) == 0) positive = 0;
            else positive = 1;
            if ((status & 0x0040) == 0) negetive = 0;
            else negetive = 1;
            if ((status & 0x0080) == 0) st = 0;
            else st = 1;
            if ((status & 0x0100) == 0) et = 0;
            else et = 1;
            if ((status & 0x0200) == 0) en = 0;
            else en = 1;
            if ((status & 0x0400) == 0) run = 0;
            else run = 1;
            if ((status & 0x0800) == 0) compare = 0;
            else compare = 1;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            axisS = Convert.ToInt16(cmb_axisStatus.SelectedIndex + 1);
            sRtn = mc.GT_GetPrfMode(0, axisS, out prfMode, 1, out pClock);
            sRtn = mc.GT_GetPrfPos(0, axisS, out prfPos, 1, out pClock);
            sRtn = mc.GT_GetPrfVel(0, axisS, out prfVel, 1, out pClock);
            sRtn = mc.GT_GetEncPos(0, axisS, out encPos, 1, out pClock);
            sRtn = mc.GT_GetEncVel(0, axisS, out encVel, 1, out pClock);
            txt_runMode.Text = prfMode.ToString();
            switch (prfMode)
            {
                case 0: txt_runMode.Text = "点位运动";
                    break;
                case 1: txt_runMode.Text = "JOG运动";
                    break;
                case 2: txt_runMode.Text = "PT运动";
                    break;
                case 3: txt_runMode.Text = "GEAR运动";
                    break;
                case 4: txt_runMode.Text = "FOLLOW运动";
                    break;
                case 5: txt_runMode.Text = "插补运动";
                    break;
                case 6: txt_runMode.Text = "PVT运动";
                    break;
                default: txt_runMode.Text = "无运动";
                    break;
            }
            txt_MprfPosition.Text = prfPos.ToString();
            txt_MprfVel.Text = prfVel.ToString();
            txt_MencPosition.Text = encPos.ToString();
            txt_MencVel.Text = encVel.ToString();
            //状态字查询
            sRtn = mc.GT_GetSts(0, axisS, out status, 1, out pClock);
            StatusConvert();
            txt_Bit1.Text = Convert.ToString(alarm);
            txt_Bit4.Text = Convert.ToString(error);
            txt_Bit5.Text = Convert.ToString(positive);
            txt_Bit6.Text = Convert.ToString(negetive);
            txt_Bit7.Text = Convert.ToString(st);
            txt_Bit8.Text = Convert.ToString(et);
            txt_Bit9.Text = Convert.ToString(en);
            txt_Bit10.Text = Convert.ToString(run);
            txt_Bit11.Text = Convert.ToString(compare);
            //sRtn = mc.GT_GetDi(0, mc.MC_ALARM,out pAlarm);
            //sRtn = mc.GT_GetDi(0, mc.MC_HOME, out pHome);
            //sRtn = mc.GT_GetDi(0, mc.MC_LIMIT_POSITIVE, out pLmtP);
            //sRtn = mc.GT_GetDi(0, mc.MC_LIMIT_NEGATIVE, out pLmtN);
            gCard.ReadIO(0, mc.MC_ALARM, (short)(axisS-1), out pAlarm);
            gCard.ReadIO(0, mc.MC_HOME, (short)(axisS-1), out pHome);
            gCard.ReadIO(0, mc.MC_LIMIT_POSITIVE, (short)(axisS - 1), out pLmtP);
            gCard.ReadIO(0, mc.MC_LIMIT_NEGATIVE, (short)(axisS - 1), out pLmtN);
            if (pAlarm == 1) //默认取反
            {
                pic_alarm.BackgroundImage = Resource._red;
                flagAlarm = 1;
            }
            else if (flagAlarm == 0)
            { pic_alarm.BackgroundImage = Resource._green; }

            if (pHome == 0)
            { pic_home.BackgroundImage = Resource._red; }
            else
            { pic_home.BackgroundImage = Resource._green; }

            if (pLmtP == 1)//默认取反
            { 
                pic_lmtP.BackgroundImage = Resource._red;
                flagLmtP = 1;
            }
            else if(flagLmtP == 0)
            { pic_lmtP.BackgroundImage = Resource._green; }

            if (pLmtN == 1)//默认取反
            { 
                pic_lmtN.BackgroundImage = Resource._red;
                flagLmtN = 1;
            }
            else if (flagLmtN == 0)
            { pic_lmtN.BackgroundImage = Resource._green; }

        }
    }
}
